﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DataAccess
{
    public class MonHoc
    {

        
        private string _maMH;
        public string MaMH
        {
            get { return _maMH; }
            set { _maMH = value; }
        }

        private string _tenMH;
        public string TenMH
        {
            get { return _tenMH; }
            set { _tenMH = value; }
        }

        public int SoTC { get; set; }

        public int TCLT { get; set; }
        public int TCTH { get; set; }
        public bool BatBuoc { get; set; }
        public string MoTaMH { get; set; }

        public MonHoc() { }
        public MonHoc(string ma, string ten)
        {
            MaMH = ma;
            TenMH = ten;
        }

        public MonHoc(MonHoc mh)
        {
            MaMH = mh.MaMH;
            TenMH = mh.TenMH;
        }

        private static SqlCommand cmd;
        private static SqlConnection cnn;
        private static void MyCommand(string text)
        {
            cmd = cnn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = text;
        }

        public static MonHoc GetData(SqlDataReader reader)
        {
            MonHoc data = new MonHoc();
            data.MaMH = CType.Tostring(reader["MaMH"]);
            data.TenMH = CType.Tostring(reader["TenMH"]);
            data.SoTC = CType.ToInt32(reader["SoTC"]);
            data.TCLT = CType.ToInt32(reader["TCLT"]);
            data.TCTH = CType.ToInt32(reader["TCTH"]);
            data.BatBuoc = CType.ToBool(reader["BatBuoc"]);
            data.MoTaMH = CType.Tostring(reader["MoTaMH"]);
            return data;
        }


        public static List<MonHoc> All()
        {
            List<MonHoc> list = new List<MonHoc>();
            using (cnn = MyConnection.MyConn())
            {
                MyCommand("MonHoc_All");
                cnn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        MonHoc data = GetData(reader);
                        list.Add(data);
                    }
                }
            }
            return list;
        }
        public static int Add(MonHoc mh)
        {
            using (SqlConnection connec = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Add");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mh.MaMH;
                cmd.Parameters.Add("@TenMH", SqlDbType.VarChar, 200).Value = mh.TenMH;

                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }

        public static string Them(MonHoc data)
        {
            using (cnn = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Add");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = data.MaMH;
                cmd.Parameters.Add("@TenMH", SqlDbType.NVarChar, 200).Value = data.TenMH;
                cmd.Parameters.Add("@SoTC", SqlDbType.Int).Value = data.SoTC;
                cnn.Open();
                int rs = (int)cmd.ExecuteNonQuery();
                cnn.Close();
                if (rs > 0)
                    return (string)cmd.Parameters["@MaMH"].Value;
                else
                    return "";
            }

           
        }

        public static string Xoa(string ma)
        {
            using (cnn = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Delete");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = ma;
                cnn.Open();
                int rs = (int)cmd.ExecuteNonQuery();
                cnn.Close();
                if (rs > 0)
                    return (string)cmd.Parameters["@MaMH"].Value;
                else
                    return "";
            }
            
        }
        public static int Delete(string Ma)
        {
            using (SqlConnection connec = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Delete");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = Ma;
                connec.Open();
                int num = cmd.ExecuteNonQuery();
                return num;
            }
        }

        public static int Sua(MonHoc data)
        {
            using (cnn = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Update");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = data.MaMH;
                cmd.Parameters.Add("@TenMH", SqlDbType.NVarChar, 200).Value = data.TenMH;
                cmd.Parameters.Add("@SoTC", SqlDbType.Int).Value = data.SoTC;
                cnn.Open();
                int rs = (int)cmd.ExecuteNonQuery();
                cnn.Close();
                if (rs > 0)
                    return (int)cmd.Parameters["@MaMH"].Value;
                else return 0;
            }
        }

        public static int Update(MonHoc mh)
        {
            using (SqlConnection connec = MyConnection.MyConn())
            {
                MyCommand("MonHoc_Update");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = mh.MaMH;
                cmd.Parameters.Add("@TenMH", SqlDbType.VarChar, 200).Value = mh.TenMH;
                connec.Open();
                int after = cmd.ExecuteNonQuery();
                return after;
            }
        }

        public static MonHoc TimMHTheoMa(string ma)
        {
            using (cnn = MyConnection.MyConn())
            {
                MyCommand("MonHoc_TimTheoMa");
                cmd.Parameters.Add("@MaMH", SqlDbType.VarChar, 10).Value = ma;
                cnn.Open();
                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        MonHoc data = GetData(reader);
                        return data;
                    }
                    else return null;
                }
            }
        }

        public static List<MonHoc> Find_ByName(string name)
        {
            List<MonHoc> list = new List<MonHoc>();
            using (SqlConnection connec = MyConnection.MyConn())
            {
                MyCommand("MonHoc_FindByName");
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = name;
                connec.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        MonHoc mh = GetMH(reader);
                        list.Add(mh);
                    }
                }

            }
            return list;
        }

        private static MonHoc GetMH(SqlDataReader reader)
        {
            MonHoc mh = new MonHoc();
            mh.MaMH = reader["MaMH"].ToString();
            mh.TenMH = reader["TenMH"].ToString();
            return mh;
        }
    }
}
